package edu.hwadee.action;


import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ModelDriven;

import edu.hwadee.entity.Car;
import edu.hwadee.entity.Company;
import edu.hwadee.entity.Material;
import edu.hwadee.entity.Supplierprice;


/**
* @author luhaojian
* @version ����ʱ�䣺2018��8��9�� ����2:26:43
* 
*/
public class CarAction extends BasicAction implements ModelDriven<Car>,RequestAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int carId;
	private Car car;
	private Integer address;
	private Integer carComplementId;
	private Integer staffId;
	private Integer companyByHoldId;
	private Integer companyByControlCompanyId;
	private Integer companyByDealerId;
	private Integer option;
	private String optionvalue;
	public CarAction() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	* @author luhaojian
	* @version ����ʱ�䣺2018��8��9�� ����2:37:04
	* ���ܣ���ӳ�����Ϣ
	 */
	public String addCar(){
		
	if(car!=null) {
		
			if(address!=null)
			{
				car.setArea(getAreaServiceImpl().searchById(address));
			}
			if(carComplementId!=null) {
				car.setCarcomplement(getCarComplementServiceImpl().search(carComplementId));
			}
			if(staffId!=null)
			{
				car.setStaff(getStaffServiceImpl().searchById(staffId));
			}
			if(companyByHoldId!=null)
			{
				car.setCompanyByHold(getCompanyServiceImpl().searchById(companyByHoldId));
			}
			if(companyByControlCompanyId!=null)
			{
				car.setCompanyByControlCompany(getCompanyServiceImpl().searchById(companyByControlCompanyId));
			}
			if(companyByDealerId!=null)
			{
				car.setCompanyByDealer(getCompanyServiceImpl().searchById(companyByDealerId));
			}
		}
		
		if(getCarServiceImpl().add(car)){
			System.out.println("����ɹ�");
			return "success";
		}
		else {
			return "error";
		}
	}
	
	public String searchCar(){
		
		if(option == 1) {
			car.setCarId(Integer.parseInt(optionvalue));
		}
		else if(option == 2) {
			car.setCarcomplement(getCarComplementServiceImpl().search(Integer.parseInt(optionvalue)));
		
		}
		else {
			car.setStaff(getStaffServiceImpl().searchById(Integer.parseInt(optionvalue)));
		}
		
		List<Car> carlist = getCarServiceImpl().searchList(car);
		ServletActionContext.getRequest().getSession().setAttribute("carList", carlist);
		ServletActionContext.getRequest().getSession().setAttribute("dooption", true);

		if(option == 1) {
			car.setCarId(null);
		}
		else if(option == 2) {
			car.setCarcomplement(null);
		}
		else {
			car.setStaff(null);
		}
		return "success";
	}
	
	public String updateCar(){
		if(car!=null) {

			if(address!=null)
			{
				car.setArea(getAreaServiceImpl().searchById(address));
			}
			if(carComplementId!=null) {
				car.setCarcomplement(getCarComplementServiceImpl().search(carComplementId));
			}
			if(staffId!=null)
			{
				car.setStaff(getStaffServiceImpl().searchById(staffId));
			}
			if(companyByHoldId!=null)
			{
				car.setCompanyByHold(getCompanyServiceImpl().searchById(companyByHoldId));
			}
			if(companyByControlCompanyId!=null)
			{
				car.setCompanyByControlCompany(getCompanyServiceImpl().searchById(companyByControlCompanyId));
			}
			if(companyByDealerId!=null)
			{
				car.setCompanyByDealer(getCompanyServiceImpl().searchById(companyByDealerId));
			}
		}
		if(getCarServiceImpl().update(car))
			return "success";
		else {
			return "error";
		}
	}
	
	public String deleteCar(){
		if(getCarServiceImpl().delete(car)){
			return "success";
		}
		else {
			return "error";
		}
	}
	
	public String showAll(){
		//System.out.println( getCarServiceImpl());
		List<Car> list=(List<Car>) getCarServiceImpl().getList();
		ServletActionContext.getRequest().getSession().setAttribute("carList", list);
			return "success";
		
	}
	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	@Override
	public Car getModel() {
		if(car==null)
			car=new Car();
		return car;
	}
	
	
	@Override
	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}

	public int getCarId() {
		return carId;
	}

	public void setCarId(int carId) {
		this.carId = carId;
	}

	public Integer getCarComplementId() {
		return carComplementId;
	}

	public void setCarComplementId(Integer carComplementId) {
		this.carComplementId = carComplementId;
	}

	public Integer getStaffId() {
		return staffId;
	}

	public void setStaffId(Integer staffId) {
		this.staffId = staffId;
	}

	public Integer getCompanyByHoldId() {
		return companyByHoldId;
	}

	public void setCompanyByHoldId(Integer companyByHoldId) {
		this.companyByHoldId = companyByHoldId;
	}

	public Integer getCompanyByControlCompanyId() {
		return companyByControlCompanyId;
	}

	public void setCompanyByControlCompanyId(Integer companyByControlCompanyId) {
		this.companyByControlCompanyId = companyByControlCompanyId;
	}

	public Integer getCompanyByDealerId() {
		return companyByDealerId;
	}

	public void setCompanyByDealerId(Integer companyByDealerId) {
		this.companyByDealerId = companyByDealerId;
	}

	public Integer getOption() {
		return option;
	}

	public void setOption(Integer option) {
		this.option = option;
	}

	public String getOptionvalue() {
		return optionvalue;
	}

	public void setOptionvalue(String optionvalue) {
		this.optionvalue = optionvalue;
	}

	public Integer getAddress() {
		return address;
	}

	public void setAddress(Integer address) {
		this.address = address;
	}




}
